/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.purchasetickets;

import assignment_cp3120.startup.StartupView;
import assignment_cp3120.ucm.UCPurchaseTickets;
import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.dataObjects.Ticket;
import assignment_cp3120.dataObjects.Flight;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JList;
import java.util.Random;
import javax.swing.JOptionPane;
/**
 *
 * @author jc148311
 */
public class PurchaseTicketsView {
    private JPanel mainPanel;
    private JPanel optionPanel;
    private JPanel titlePanel;
    private JPanel buttonPanel;
    private JLabel title;
    private JButton backButton;
    private JButton buyButton;
    private JScrollPane scrollPane;
    private ButtonGroup group;
    private JList listF;
    private JPanel previousPanel;

    private PurchaseTicketsModel model;

    private Vector<Ticket> ticketList;
    private Vector<Flight> flightList;
    private Vector<JRadioButton> buttonList;

    private Ticket ticket;

    public PurchaseTicketsView() {
        this(new PurchaseTicketsModel());
    }

    public PurchaseTicketsView(PurchaseTicketsModel model) {
        this.model = model;
        createPurchaseGUI();
    }
    
    private Flight flight;

    public void createPurchaseGUI() {
        flight = new Flight();
        ticket = new Ticket();

        mainPanel = new JPanel();
        titlePanel = new JPanel();
        optionPanel = new JPanel();
        title = new JLabel("Purchase Tickets");
        backButton = new JButton("Back");
        buyButton = new JButton("Buy");
        buttonPanel = new JPanel();
        scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        buttonList = new Vector();
        group = new ButtonGroup();

        titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.PAGE_AXIS));
        titlePanel.add(title);

        ticketList = model.getTicketList();        
        listF = new JList(flightList);
        scrollPane.add(listF);

        optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.PAGE_AXIS));

        createFlightList();

        buttonClick al = new buttonClick();
        backButton.addActionListener(al);
        buyButton.addActionListener(al);
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.add(buyButton);
        buttonPanel.add(backButton);        
        
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(titlePanel);
        mainPanel.add(buttonPanel);
        mainPanel.add(optionPanel);
        mainPanel.setVisible(true);
    }

    class buttonClick implements ActionListener {

        public void actionPerformed(ActionEvent e)    {
            int i = 0;
            if(e.getSource() == buyButton) {
                while(true) {
                    if(buttonList.get(i).isSelected()) {
                        Flight ft = flightList.get(i);
                        int ref = new Random().nextInt(100000);
                        buyTicket(ft.getFlight_id(), String.valueOf(ref), "0" , ft.getFlight_price());
                        JOptionPane.showConfirmDialog(mainPanel, "Ticket Reference Number: " + ref, "Ticket Bought", JOptionPane.PLAIN_MESSAGE);
                        System.out.println(i);
                        break;
                    }
                    i++;
                }
            }
            if(e.getSource() == backButton) {
                StartupView.getInstance().setContents(previousPanel);
            }
        }
    }

    public void showGUI() {
        previousPanel = StartupView.getInstance().getContents();
        createFlightList();
        StartupView.getInstance().setContents(mainPanel);
    }

    public String formatTicketDetails(Flight flight) {
        String details = new String();
        details = "Flight ID: " + flight.getFlight_id() + "      to " + flight.getDest_id() + " from " + flight.getDep_id() + " departing time at " + flight.getDep_time() + "  $" + flight.getFlight_price();
        return details;
    }

    public void createFlightList() {
        flightList = model.getFlightList();
        for(int i = 0; i < flightList.size(); i++) {
            flight = flightList.get(i);
            buttonList.add(new JRadioButton(formatTicketDetails(flight)));
            group.add(buttonList.get(i));
            optionPanel.add(buttonList.get(i));
        }
    }

    public void buyTicket(String flightID, String refID, String transID, int price) {
        Ticket ticket = new Ticket();
        ticket.setFlightID(flightID);
        ticket.setRefID(refID);
        ticket.setTransactionID(transID);
        ticket.setTicketPrice(price);

        model.updateTicketDB(ticket);
    }
}
